<?php

    $monStDate = date('Y-m-1');//本月一号
    $monStTime = strtotime($monStDate);//本月一号对应时间戳
    $weekMonSt = date('N',$monStTime);//本月一号星期几
    $monDays = date('t',$monStTime);
    $result = [];
    $n = 1;
    for($i=1;$i<=$monDays;$i++){
        $k = $i/7;
        if($k>$n){
            $n++;
        }
        $today = date('Y-m-'.$i);
        $nextDay = date('Y-m-d',strtotime($today)+86400);
        $yesToday = date('Y-m-d',strtotime($today)-86400);
        $result[$n][$weekMonSt] =[
                'path'=>$n."-".$weekMonSt,
                'w'=>$i,
                'ym'=>date('Y-n'),
                'today'=>$today,
                'next_date'=>$nextDay,
                'yestoday'=>$yesToday
                ];
        $weekMonSt++;
        if($weekMonSt>7){
            $n++;
            $weekMonSt = 1;
        }

    }
    for ($j=1;$j<=7;$j++){
        $line1 = $result[1];
        $line5 = $result[5];
        if(!isset($line1[$j])){
            $result[1][$j] = '';
        }
        if(!isset($line5[$j])){
            $result[5][$j] = '';
        }
    }
    foreach ($result as $line=>$items ){
        foreach ($items as $w=>$day){
            if($line-1>0){
                if(!isset($result[$line-1])){
                    continue;
                }
                $demo = $result[$line-1];
                if(!isset($demo[$w])){
                    continue;
                }
                $to = $demo[$w];
                if(is_array($to)&&is_array($day)){
                    $day['up_day'] = $to['today'];
                }

            }else{
                if(is_array($day)){
                    $day['up_day'] = '';
                }

            }
            if($line+1<=$n){
                if(!isset($result[$line+1])){
                    continue;
                }
                $demo = $result[$line+1];
                if(!isset($demo[$w])){
                    continue;
                }
                $to = $demo[$w];
                if(is_array($to)&&is_array($day)){
                    $day['down_day'] = $to['today'];
                }
            }else{
                if(is_array($day)){
                    $day['down_day'] = '';
                }
            }
            $items[$w] = $day;
        }
        $result[$line] = $items;

    }
$file = 'table.php';
extract($result);
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean();
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加班时长计算</title>
    <link type="text/css" rel="stylesheet" href="css/index.css"/>
    <script type="text/javascript" src="js/index.js?t=<?php echo time();?>"></script>
    <script type="text/javascript" src="js/date.js?t=<?php echo time();?>"></script>
</head>
<body>
<div class="header">
    <div class="btn1">
        <button id="btn2" ">上次填写</button><button id="btn" ">提交</button><button id="btn3" ">清空表单</button>
    </div>
    <div class="select">
        <form action="select.php" method="post" target="f2" id="from2" >
            <select name="month" id="select1">
                <option value="">请选择</option>
                <?php for($i=1;$i<=date('m');$i++):?>
                    <option value="<?php echo date('Y-').(int)$i;?>"  <?php if(date('m')==$i):?> selected <?php endif;?> ><?php echo date('Y');?>年<?php echo $i;?>月</option>
                <?php endfor; ?>
            </select>
        </form>

    </div>
    <br clear="both">
</div>


<div class="main">
    <h1 style="text-align: center; margin-bottom: 30px" id="h1"><?php echo date('Y');?>年<?php echo date('m');?>月</h1>
    <form action="sign.php" method="post" target="f1" id="from1" >
        <?php echo $contents;?>
    </form>
    <iframe id="f1" name="f1" style="width: 1px;height: 1px;"></iframe>
    <iframe id="f2" name="f2" style="width: 1px;height: 1px;"></iframe>

</div>
</body>
</html>
